package com.sloth.bear.util;

import com.sloth.bear.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class BaseHelper {

    public static <T, R> T r2t(R resource, Class<T> target) {
        if (resource == null) {
            return null;
        }
        try {
            T t = target.newInstance();
            BeanUtils.copyProperties(resource, t);
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new BusinessException("bean转化异常", e);
        }
    }

    public static <T, R> List<T> r2t(List<R> resourceList, Class<T> target) {
        List<T> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(resourceList)) {
            resourceList.forEach(resource -> {
                T t1 = r2t(resource, target);
                list.add(t1);
            });
        }
        return list;
    }
}
